/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name = "PageController",
loadOnStartup = 1,
urlPatterns = {"/home",
    "/chooseLanguage",
    "/services",
    "/Public/Shop"})
public class PageController extends HttpServlet {

    private String sampleServletContext;

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

        super.init(servletConfig);

        // initialize servlet with configuration information
        sampleServletContext = servletConfig.getServletContext().getInitParameter("sampleServletContext");
    }

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String userPath = request.getServletPath();
        HttpSession session = request.getSession();

        // if category page is requested
        if (userPath.equals("/home")) {
            userPath = "/home";
        } else if (userPath.equals("/services")) {
            userPath = "/services";

        } else if (userPath.equals("/chooseLanguage")) {
            // get language choice
            String language = request.getParameter("language");

            // place in request scope
            request.setAttribute("language", language);
            // forward request to welcome page
            try {
                request.getRequestDispatcher("/index.jsp").forward(request, response);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return;
        } else if (userPath.equals("/login/login_panel")) {
            userPath = "/WEB-INF/view/login/login_panel";
        } else if (userPath.equals("/login/login")) {
            userPath = "/WEB-INF/view/login/login";
        } else if (userPath.equals("/Public/Shop")) {
            userPath = "/WEB-INF/view/Public/Shop";
        }
        // use RequestDispatcher to forward request internally
        String url = "" + userPath + ".jsp";

        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }
}
